package com.zhiche.lisa.bms.controller.bill;

import com.zhiche.lisa.bms.dao.model.interfacelog.InterfaceLog;
import com.zhiche.lisa.bms.dao.model.price.PriceIncoming;
import com.zhiche.lisa.bms.pojo.dto.bill.BillIoDTO;
import com.zhiche.lisa.bms.service.bill.BillSynchroService;
import com.zhiche.lisa.bms.service.interfacelog.InterfaceLogService;
import com.zhiche.lisa.bms.service.utils.InterfaceLogUtil;
import com.zhiche.lisa.core.enums.CommonEnum;
import com.zhiche.lisa.core.supports.BaseException;
import com.zhiche.lisa.core.supports.RestfulResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: WangShuiJin
 * @Description: 同步账单
 * @Date: Create in 17:13 2018/9/7
 */
@RestController
@RequestMapping("/bill")
@Api(value = "/bill", description = "同步账单数据", tags = {"同步账单数据"})
public class BillSynchroController {
    private static final Logger LOGGER = LoggerFactory.getLogger(BillSynchroController.class);
    @Autowired
    BillSynchroService billSynchroService;

    @Autowired
    private InterfaceLogUtil interfaceLogUtil;

    @PostMapping(value = "/synchroBillData")
    @ApiOperation(value = "同步账单数据", notes = "主系统推送账单数据，此接口负责多表的数据插入", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = PriceIncoming.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<String> synchroBillData(@RequestBody BillIoDTO billIoDTO) {
        LOGGER.info("BillIoAndDetailController.synchroBillData（同步账单数据） params : {} ", billIoDTO);
        RestfulResponse<String> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        InterfaceLog interfaceLog = new InterfaceLog();
        try {
            interfaceLogUtil.setCalleeCommonLog(interfaceLog, billIoDTO, "/bill/synchroBillData");
            billSynchroService.synchroBillData(billIoDTO);
            interfaceLogUtil.setSuccessLog(interfaceLog);
        } catch (BaseException e) {
            LOGGER.error("BillIoAndDetailController.synchroBillData ERROR {}", e);
            interfaceLogUtil.setErrorLog(interfaceLog, e.getMessage());
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("BillIoAndDetailController.synchroBillData ERROR {}", e);
            interfaceLogUtil.setErrorLog(interfaceLog, e.getMessage());
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        // 保存日志
        interfaceLogUtil.saveInterfaceLog(interfaceLog);
        return result;
    }
}
